//
//  LLSegmentTableView.m
//  TaoTuo
//
//  Created by admin on 2019/8/20.
//  Copyright © 2019 admin. All rights reserved.
//

#import "LLSegmentTableView.h"
#import "LLLeftSegViewCell.h"
#import "LLRightSegViewCell.h"

static NSString *leftCellIdentifier = @"seg_left_cell_identifier";
static NSString *rightCellIdentifier = @"seg_right_cell_identifier";

@interface LLSegmentTableView ()<UITableViewDataSource, UITableViewDelegate>
{
    UITableView *leftTableView;
    UITableView *rightTableView;
    NSInteger _count;   //分区数
    BOOL isRight;
}
@property (nonatomic , strong) NSArray *titleArray;  //标题
@property (nonatomic , strong) NSArray *detailArray;

@end

@implementation LLSegmentTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 4, self.frame.size.height) style:UITableViewStylePlain];
        leftTableView.delegate = self;
        leftTableView.dataSource = self;
        leftTableView.tag = 100;
        leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [leftTableView registerClass:[LLLeftSegViewCell class] forCellReuseIdentifier:leftCellIdentifier];
        [self addSubview:leftTableView];
        leftTableView.tableFooterView = [UIView new];
        leftTableView.backgroundColor = kSeparatorColor;
        
        rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftTableView.frame), 0, self.frame.size.width * 3 / 4, self.frame.size.height) style:UITableViewStylePlain];
        rightTableView.delegate = self;
        rightTableView.dataSource = self;
        rightTableView.tag = 200;
        rightTableView.rowHeight = 100;
        rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        rightTableView.tableFooterView = [UIView new];
        [rightTableView registerClass:[LLRightSegViewCell class] forCellReuseIdentifier:rightCellIdentifier];
        [self addSubview:rightTableView];
        
        isRight = YES;
        
        
    }
    return self;
}

- (void)setTitleArray:(NSArray *)titleArray detailArray:(NSArray *)detailArray
{
    self.titleArray = titleArray;
    self.detailArray = detailArray;
    
    [leftTableView reloadData];
    [rightTableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == leftTableView) {
        return 1;
    }
    _count = self.titleArray.count;
    return _count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == leftTableView) {
        return self.titleArray.count;
    }
    
    return [self.detailArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (tableView == leftTableView) {
        
        LLLeftSegViewCell *cell = [tableView dequeueReusableCellWithIdentifier:leftCellIdentifier];
        cell.title = self.titleArray[indexPath.row];
        return cell;
    } else {
        LLRightSegViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rightCellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.detailArray[indexPath.section][indexPath.row];
        return cell;
    }
    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //如果选中左边的
    if (tableView == leftTableView) {
        isRight = NO;
        //让右边相应的区头滚动到上部
        [rightTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] animated:YES scrollPosition:UITableViewScrollPositionTop];
    } else if (tableView == rightTableView) {
        //右边的
        if ([self.delegate respondsToSelector:@selector(view:didSelectRowAtIndexPath:model:)]) {
            [self.delegate view:self didSelectRowAtIndexPath:indexPath model:self.detailArray[indexPath.section][indexPath.row]];
        }
    }
}


//区头标题
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView == rightTableView) {
        NSString *str = [NSString stringWithFormat:@"— %@ —", self.titleArray[section]];
        UILabel *label = [UILabel labelTextCenterWithTitle:@"" color:UIColorHex(C8C8C8) fontSize:16];
        label.frame = CGRectMake(0, 0, rightTableView.frame.size.width, 30);
        label.backgroundColor = [UIColor whiteColor];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attrStr addAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName : UIColorHex(333333)} range:[str rangeOfString:self.titleArray[section]]];
        
        label.attributedText = attrStr;
        
        return label;
    }
    return nil;
}

//区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == rightTableView) {
        return 30;
    }
    return 0;
}

//返回每一个区尾的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(tableView == leftTableView) {
        return 0;
    }
    return 10;   //这个太小的话，就没反应了
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (tableView == rightTableView) {
        return [[UIView alloc] initWithFrame:CGRectMake(0, 0, rightTableView.frame.size.width, 10)];
    }
    return [UIView new];
}

//将要展示的区头视图
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if (isRight) {
        NSInteger topCellSection = [[[tableView indexPathsForVisibleRows] firstObject] section];
        NSLog(@"---------------topCellSection = %ld", topCellSection);
        if (tableView == rightTableView) {
            [leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:topCellSection inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        }
    }
}

//已经展示的区尾视图
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section
{
    if (isRight) {
        NSInteger topCellSection = [[[tableView indexPathsForVisibleRows] firstObject] section];
        NSLog(@">>>>>>>>%ld", [[[tableView indexPathsForVisibleRows] firstObject] section]);
        if (tableView == rightTableView) {
            [leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:topCellSection inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        }
    }
}


#pragma mark UISCrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    isRight = YES;
}


@end
